// pages/jinbi_qiandao/jinbi_qiandao.js
var util = require('../../utils/util.js')

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowDate: '2021',
    from: 'qiandao',
    qiandaoNum: 0,
    yaoqingNum:10,
    shareImg:"https://6d61-main-9g11cspaf548a298-1306430077.tcb.qcloud.la/cloudbase-cms/upload/2021-09-11/ly5ao1bg73md7xii9wv1v8jfflrznlc9_.jpg?sign=cbf7c166158e1b91d9f7cad5b3db0839&t=1631355785"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    this.getLookVideos_qiandao()//  1.查询今天剩余签到次数
    this.setData({
      yaoqingNum:app.globalData.jinbizhi.yaoqing,//设置分享金币值
      from: options.id, // 获取事件来源
      nowDate: util.formatTime(new Date()).slice(0,10) , // 设置今天的时间显示到页面
      shareImg:app.globalData.getGlobalShareMedia.jinbiShareLgImg,
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  onShow(options){
  },
  //  1.查询今天剩余签到次数
  getLookVideos_qiandao() {
    wx.cloud.callFunction({
      name: 'lookvideosqiandao',
      data: {
        tag: 'get'
      }
    }).then(res => {

      this.setData({
        qiandaoNum: res.result[0].qiandaoNum
      })
    })
  },
  // 2.金币+1
  // 3.签到次数置0
  // 签到
  yesQiandao(e) {
    if (e.currentTarget.dataset.tag == 'back') {
      wx.navigateToMiniProgram({
        appId: 'wxa845a8ade7205ca6',
        path: '/pages/news/news',
     
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    } else {
      // 金币+1
      // 签到次数置零
      this.setData({
        qiandaoNum:0
      })
      this.addJinbi()
      this.subQiandaoNum()
      
    }
  },
  // 金币+1
    // 签到次数置零
    // 添加金币
    addJinbi() {
      wx.cloud.callFunction({
        name: 'user',
        data: {
          tag: 'addJinbi',
          zhi: app.globalData.jinbizhi.qiandao,
          mubiao: app.globalData.userInfo._openid
        }
      }).then(res => {
      })
    },
 // 签到次数置零
    subQiandaoNum() {
      wx.cloud.callFunction({
        name: 'lookvideosqiandao',
        data: {
          tag: 'subqiandao',
        }
      }).then(res => {
        wx.showToast({
          icon:'none',
          title: `签到成功，金币+${app.globalData.jinbizhi.qiandao}`,
        })
      })
    },
  // 取消
  noQiandao() {
    wx.navigateBack({
      delta: 1,
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // title:  app.globalData.getGlobalShareMedia.jinbiShareText,
      // path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
      // imageUrl: app.globalData.getGlobalShareMedia.jinbiShareLgImg,
      title: `${app.globalData.userInfo.nickName}带你${app.globalData.getGlobalShareMedia.jinbiShareText}`,
      path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
      imageUrl: app.globalData.getGlobalShareMedia.jinbiShareLgImg,
    }
  },


})